package com.lechi.web.service.impl;

import com.lechi.web.common.enums.ErrorCodeEnum;
import com.lechi.web.common.exception.ServiceException;
import com.lechi.web.framework.log.IotExceptionLog;
import com.lechi.web.service.IIotAlertRecordService;
import com.lechi.web.system.domain.IotAlertRecord;
import com.lechi.web.system.mapper.iot.IotAlertRecordMapper;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.concurrent.CompletableFuture;

/**
 * 告警记录Service业务层处理
 *
 * @author yangguang
 * @date 2024 -05-10
 */
@Slf4j
@Service("IotAlertRecordServicel")
@RequiredArgsConstructor
public class IotAlertRecordServiceImpl implements IIotAlertRecordService {

    private final IotAlertRecordMapper iotAlertRecordMapper;

    /**
     * 查询告警记录
     *
     * @param id 告警记录主键
     * @return 告警记录
     */
    @Override
    public IotAlertRecord selectIotAlertRecordById(Long id) {
        try {
            return iotAlertRecordMapper.selectIotAlertRecordById(id);
        } catch (Exception e) {
            StackTraceElement stackTraceElement = Thread.currentThread().getStackTrace()[1];
            String method = Thread.currentThread().getName() + ":" + stackTraceElement.getClassName() + "." + stackTraceElement.getMethodName();
            CompletableFuture.runAsync(IotExceptionLog.exceptionLog(e, "查询告警记录", method, id));
            throw new ServiceException(ErrorCodeEnum.QUERY_ERROR.getValue(), ErrorCodeEnum.QUERY_ERROR.getKey());
        }
    }

    /**
     * 查询告警记录列表
     *
     * @param iotAlertRecord 告警记录
     * @return 告警记录
     */
    @Override
    public List<IotAlertRecord> selectIotAlertRecordList(IotAlertRecord iotAlertRecord) {
        try {
            return iotAlertRecordMapper.selectIotAlertRecordList(iotAlertRecord);
        } catch (Exception e) {
            StackTraceElement stackTraceElement = Thread.currentThread().getStackTrace()[1];
            String method = Thread.currentThread().getName() + ":" + stackTraceElement.getClassName() + "." + stackTraceElement.getMethodName();
            CompletableFuture.runAsync(IotExceptionLog.exceptionLog(e, "查询告警记录列表", method, iotAlertRecord));
            throw new ServiceException(ErrorCodeEnum.QUERY_ERROR.getValue(), ErrorCodeEnum.QUERY_ERROR.getKey());
        }
    }

    /**
     * 新增告警记录
     *
     * @param iotAlertRecord 告警记录
     */
    @Override
    public void insertIotAlertRecord(IotAlertRecord iotAlertRecord) {
        iotAlertRecord.setAlertTime(new Date());
        try {
            iotAlertRecordMapper.insertIotAlertRecord(iotAlertRecord);
        } catch (Exception e) {
            StackTraceElement stackTraceElement = Thread.currentThread().getStackTrace()[1];
            String method = Thread.currentThread().getName() + ":" + stackTraceElement.getClassName() + "." + stackTraceElement.getMethodName();
            CompletableFuture.runAsync(IotExceptionLog.exceptionLog(e, "新增告警记录", method, iotAlertRecord));
            throw new ServiceException(ErrorCodeEnum.ADD_ERROR.getValue(), ErrorCodeEnum.ADD_ERROR.getKey());
        }
    }

    /**
     * 修改告警记录
     *
     * @param iotAlertRecord 告警记录
     * @return 结果
     */
    @Override
    public int updateIotAlertRecord(IotAlertRecord iotAlertRecord) {
        try {
            return iotAlertRecordMapper.updateIotAlertRecord(iotAlertRecord);
        } catch (Exception e) {
            StackTraceElement stackTraceElement = Thread.currentThread().getStackTrace()[1];
            String method = Thread.currentThread().getName() + ":" + stackTraceElement.getClassName() + "." + stackTraceElement.getMethodName();
            CompletableFuture.runAsync(IotExceptionLog.exceptionLog(e, "修改告警记录", method, iotAlertRecord));
            throw new ServiceException(ErrorCodeEnum.UPDATE_ERROR.getValue(), ErrorCodeEnum.UPDATE_ERROR.getKey());
        }
    }

    /**
     * 删除告警记录
     *
     * @param iotAlertRecord 告警记录
     */
    @Override
    public void deleteIotAlertRecord(IotAlertRecord iotAlertRecord) {
        try {
            iotAlertRecordMapper.deleteIotAlertRecord(iotAlertRecord);
        } catch (Exception e) {
            StackTraceElement stackTraceElement = Thread.currentThread().getStackTrace()[1];
            String method = Thread.currentThread().getName() + ":" + stackTraceElement.getClassName() + "." + stackTraceElement.getMethodName();
            CompletableFuture.runAsync(IotExceptionLog.exceptionLog(e, "按条件删除告警记录", method, iotAlertRecord));
            throw new ServiceException(ErrorCodeEnum.DELETE_ERROR.getValue(), ErrorCodeEnum.DELETE_ERROR.getKey());
        }
    }

    /**
     * 批量删除告警记录
     *
     * @param ids 需要删除的告警记录主键
     * @return 结果
     */
    @Override
    public int deleteIotAlertRecordByIds(Long[] ids) {
        try {
            return iotAlertRecordMapper.deleteIotAlertRecordByIds(ids);
        } catch (Exception e) {
            StackTraceElement stackTraceElement = Thread.currentThread().getStackTrace()[1];
            String method = Thread.currentThread().getName() + ":" + stackTraceElement.getClassName() + "." + stackTraceElement.getMethodName();
            CompletableFuture.runAsync(IotExceptionLog.exceptionLog(e, "删除告警记录", method, ids));
            throw new ServiceException(ErrorCodeEnum.DELETE_ERROR.getValue(), ErrorCodeEnum.DELETE_ERROR.getKey());
        }
    }

    /**
     * 批量删除告警记录
     *
     * @param deviceId 需要删除的告警记录主键
     */
    @Override
    public void deleteIotAlertRecordByDeviceId(Long deviceId) {
        try {
            iotAlertRecordMapper.deleteIotAlertRecordByDeviceId(deviceId);
        } catch (Exception e) {
            StackTraceElement stackTraceElement = Thread.currentThread().getStackTrace()[1];
            String method = Thread.currentThread().getName() + ":" + stackTraceElement.getClassName() + "." + stackTraceElement.getMethodName();
            CompletableFuture.runAsync(IotExceptionLog.exceptionLog(e, "通过设备ID删除告警记录", method, deviceId));
            throw new ServiceException(ErrorCodeEnum.DELETE_ERROR.getValue(), ErrorCodeEnum.DELETE_ERROR.getKey());
        }
    }

    /**
     * 通过传感器ID删除告警记录
     *
     * @param SensorAliasId 需要删除的告警记录主键
     */
    public void deleteIotAlertRecordBySensorAliasId(Long SensorAliasId) {
        try {
            iotAlertRecordMapper.deleteIotAlertRecordBySensorAliasId(SensorAliasId);
        } catch (Exception e) {
            StackTraceElement stackTraceElement = Thread.currentThread().getStackTrace()[1];
            String method = Thread.currentThread().getName() + ":" + stackTraceElement.getClassName() + "." + stackTraceElement.getMethodName();
            CompletableFuture.runAsync(IotExceptionLog.exceptionLog(e, "通过传感器ID删除告警记录", method, SensorAliasId));
            throw new ServiceException(ErrorCodeEnum.DELETE_ERROR.getValue(), ErrorCodeEnum.DELETE_ERROR.getKey());
        }
    }
}
